package rocketmq.demo.oneway.trans;

import rocketmq.demo.utils.MqConfig;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.common.RemotingHelper;

/**
 * <pre>
 * One-way transmission 单向传输
 * 
 * Application: One-way transmission is used for cases
 * requiring moderate reliability, such as log collection.
 * </pre>
 */
public class OnewayProducer {
	public static void main(String[] args) throws Exception {
		// Instantiate with a producer group name.
		DefaultMQProducer producer = new DefaultMQProducer(MqConfig.GROUP_TEST);

		// 指定nameServer的地址  测试时在代码中指定
		producer.setNamesrvAddr(MqConfig.DEFAULT_NAME_SRV_ADDR);

		// Launch the instance.
		producer.start();
		for (int i = 0; i < 30; i++) {
			// Create a message instance, specifying topic, tag and message
			// body.
			Message msg = new Message(MqConfig.TOPIC_ONE_WAY /* Topic */,
					MqConfig.TAG_TEST /* Tag */,
					("Hello RocketMQ " + i)
							.getBytes(RemotingHelper.DEFAULT_CHARSET) /*
																	 * Message
																	 * body
																	 */
			);
			// Call send message to deliver message to one of brokers.
			producer.sendOneway(msg);

		}
		// Shut down once the producer instance is not longer in use.
		producer.shutdown();
	}
}
